package com.bookstore.dto;

import com.bookstore.entity.Category;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class CategoryDTO {
    private Long id;
    private String categoryName;
    private String description;
    private Long parentId;
    private String parentName;
    private List<CategoryDTO> children = new ArrayList<>();
    private Integer displayOrder;
    private String icon;
    
    public static CategoryDTO fromEntity(Category category) {
        if (category == null) {
            return null;
        }
        
        CategoryDTO dto = new CategoryDTO();
        dto.setId(category.getId());
        dto.setCategoryName(category.getCategoryName());
        dto.setDescription(category.getDescription());
        dto.setDisplayOrder(category.getDisplayOrder());
        dto.setIcon(category.getIcon());
        
        if (category.getParent() != null) {
            dto.setParentId(category.getParent().getId());
        }
        
        if (category.getChildren() != null && !category.getChildren().isEmpty()) {
            dto.setChildren(category.getChildren().stream()
                    .map(CategoryDTO::fromEntity)
                    .collect(Collectors.toList()));
        }
        
        return dto;
    }
}
